#include <stdio.h>
#include <string.h>
#include <math.h>

/*
 * 2010.12.29	15:59 
 *	jinghoa application
 */

#define REG_MASK(c,r,f)    c##_##r##_##f##_MASK
#define REG_SHIFT(c,r,f)   c##_##r##_##f##_SHIFT

#define GET_REG_FIELD(m,c,r,f) \
	((((m)= ((m) & REG_MASK(c,r,f)) >> REG_SHIFT(c,r,f))))

#define SET_REG_FIELD(m,c,r,f,d) \
	((m) = (((m) & ~REG_MASK(c,r,f)) | ((((d) ) << \
	 REG_SHIFT(c,r,f)) & REG_MASK(c,r,f))) \
	)

#define MASK_TC_BIT(rw, __name) ((rw & (1 << BIO_RW_ ## __name)) << \
	  (ilog2(BLK_TC_ ## __name) + BLK_TC_SHIFT - BIO_RW_ ## __name))

/* test data */
#define BCHP_NAND	20
#define CONFIG		20
#define PAGE_SIZE	4

int reg_mask()
{
	int BCHP_NAND_CONFIG_PAGE_SIZE_MASK = 10;

	printf("[%s:%d] %d\n", __FUNCTION__, __LINE__, REG_MASK(BCHP_NAND, CONFIG, PAGE_SIZE));	// 33

	return 0;
}

#define STR(s)     # s 
#define CONS(a,b)  (int)(a##e##b) 

#define TOW      (2) 
#define MUL(a,b) (a*b) 

#define A	2
#define B	3

#define  ___ANONYMOUS1(type, var, line)  type  var##line 
#define  __ANONYMOUS0(type, line)  ___ANONYMOUS1(type, _anonymous, line) 
#define  ANONYMOUS(type)  __ANONYMOUS0(type, __LINE__) 


#define  FILL(a)   {a, #a} 

enum IDD
{
	OPEN, 
	CLOSE
}; 

typedef struct MSG
{ 
	enum IDD id; 
	const char * msg; 
}MSG; 

// 把第一个参数后边加上字符_p，把第二个参数变成字符串
#define STRCPY(a,b) strcpy(a##_p,#b)  
/*
 * 如果传进来是x, 将会变成 x_mask, 程序中需要用 x_mask 申明或定义.
 */

#define _STR(a)		(a##_mask)

#define paster( n ) printf( "token" #n " = %d\n", token##n )
#define printx(n)	printf("%d\n", x ## n)

int strc()
{
	char var1_p[20];
	char var2_p[30];
	char x_mask[3] = {'a','b'};
	int token9 = 9;
	int x20 = 2;

	strcpy(var1_p,"aaaa");
	strcpy(var2_p,"bbbb");

	printf("%d : %s\n",__LINE__, var1_p);	// aaaa
	printf("%d : %s\n",__LINE__, var2_p);	// bbbb

/*
 * var1 & var2 被变成了var1_p, var2_p, 也就是上面定义的变量.
 * 后面的参数由于#的关系,变成字符串复制进了var1_p, var2_p中.
 */

	STRCPY(var1,xiaozhi);         //等于strcpy(var1_p,"var2");
	STRCPY(var2,wise);            //等于strcpy(var2_p,"var1");
	
	printf("%d : %s\n",__LINE__, var1_p);	// xiaozhi
	printf("%d : %s\n",__LINE__, var2_p);	// wise

	printf("[%s:%d] %s\n", __FUNCTION__, __LINE__,strcpy(var1_p,_STR(x)));	// ab

	paster(9);	// token9 = 9
	printx(20);	// 20 -> (20) -> x ## n -> x20 -> x20 = 2

	return 0;
}

#define HAO(f, a...)	printf(f, ##a)

int main() 
{ 
	MSG _msg[] = {FILL(OPEN), FILL(CLOSE)};
	int AeB = 4;

	printf(STR(xiaozhi));				// 输出字符串"xiaozhi" 
	printf("\n");

	printf("[line:%d]\t%d\n", __LINE__, CONS(2,3));		// 2000
	printf("[line:%d]\t%d\n", __LINE__, (int)(AeB));	// 4
	printf("%d : %d*%d=%d\n", __LINE__, TOW, TOW, MUL(TOW,TOW)); // 2*2=4

	ANONYMOUS(static int);
//	printf("%s\n", ANONYMOUS(static int));

	printf("[line:%d]\t%d %s\n", __LINE__,(int)_msg[0].id, (char*)_msg[0].msg);	// 0 OPEN

	strc();
	reg_mask();

	return 0; 
} 


